package org.jfonia.connect5.relations;

import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.connect5.basics.Node;

/**
 * @author wijnand.schepens@hogent.be
 */
public abstract class ABRelation<S, T> implements Relation
{
	protected MutableValueNode<S> a;
	protected MutableValueNode<T> b;


	public ABRelation(MutableValueNode<S> a, MutableValueNode<T> b)
	{
		this.a = a;
		this.b = b;
		switchOn();
	}

	public void switchOn()
	{
		a.addObserver(this);
		b.addObserver(this);
		updateB(); 
	}

	public void switchOff()
	{
		a.removeObserver(this);
		b.removeObserver(this);
	}

	public abstract void updateB();

	public abstract void updateA();

	public void onNotify(Object source)
	{
		if (source == a)
			updateB();
		else if (source == b)
			updateA();
		else
			throw new RuntimeException("source should be a or b");
	}
	
}
